package com.woniu.backparking.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.backparking.entity.Admin;
import com.woniu.backparking.jwtUtil.JwtUtil;
import com.woniu.backparking.service.AdminService;
import com.woniu.backparking.util.BaseEnum;
import com.woniu.backparking.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author HashSong
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
@Slf4j
public class AdminController {
    @Resource
    private AdminService adminService;
    /*
    平台管理员的登录方法:
    传参:管理员对象来自前端的有效值为管理员账号account和密码password
    return:经过加密的token   http://47.104.66.120:8081/admin/login?adminAccount=root222&password=123abc456
     */
    @GetMapping("login")
    public JSONResult login(Admin admin){
        log.info("后端登录参数查看，{},{}",admin.getAdminAccount(),admin.getPassword());
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_account",admin.getAdminAccount());
        Admin one = adminService.getOne(wrapper);
        if(admin.getPassword().equals(one.getPassword())){
            String token = JwtUtil.generatorJWT(one.getAdminId().toString());
//            System.out.println("用户密码正确");
            return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,token,null);

        }else {
            throw new RuntimeException("未知异常");
        }
    }
    //测试方法，无实际作用
    @GetMapping("/test01")
    public void test01(@RequestHeader("token")String token) throws Exception {
        log.info("测试参数查看{}",token);
        String id= JwtUtil.getId(token);
        System.out.println("进入查询user了："+id);
    }

}

